/**
 * @author LKQ
 * @date 2021/12/24 15:54
 * 结束时间：2021/12/24 16:34
 * @description 递归
 */
public class Solution {
    public static void main(String[] args) {

        TreeNode leaf1 = new TreeNode(1);
        TreeNode leaf3 = new TreeNode(3);
        TreeNode leaf6 = new TreeNode(6);
        TreeNode leaf9 = new TreeNode(9);
        TreeNode leaf2 = new TreeNode(2, leaf1, leaf3);
        TreeNode leaf7 = new TreeNode(7, leaf6, leaf9);
        TreeNode root = new TreeNode(4, leaf2, leaf7);
        Solution solution = new Solution();
        solution.invertTree(root);

    }

    public TreeNode invertTree(TreeNode root) {
        if (root == null) {
            return null;
        }
        while (root.left != null || root.right != null) {
            TreeNode temp;
            temp = root.left;
            root.left = invertTree(root.right);
            root.right = invertTree(temp);
            if (root.left != temp) {
                break;
            }
        }
        return root;
    }
}
